package com.dk.controller;

import com.dk.pojo.SslRole;
import com.dk.service.SslRoleService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("roleController")
public class RoleController {
    //使用接口： 易于扩展，可插拔性高
    @DubboReference
    private SslRoleService roleService;

    @RequestMapping("list")
    public String list(){
        return "role/list";
    }
    //查询角色列表（全部）
    @RequestMapping("findRoleList")
    @ResponseBody
    public List<SslRole> findRoleList(){
        List<SslRole> roles = roleService.findRoleList();
        return roles;
    }

    //跳转到添加角色页面
    @RequestMapping("toAddRole")
    public String toAddRole(){
        return "role/add";
    }

    @RequestMapping("saveRole")
    @ResponseBody
    public boolean saveRole(SslRole trole,@RequestParam("nodesIds[]") Integer[] nodesIds){
        roleService.saveRole(trole,nodesIds);
        return true;
    }
    //修改回填
    @RequestMapping("toUpdate/{roleId}")
    public String toUpdate(@PathVariable("roleId") Integer roleId, Model model){
        //1、根据角色id，查询当前角色对象
        SslRole role = roleService.findRoleById(roleId);
        model.addAttribute("role",role);
        return "role/update";
    }
    //修改保存,健壮性
    @RequestMapping("updateRole")
    @ResponseBody
    public boolean updateRole(SslRole trole,@RequestParam("nodesIds[]") Integer[] nodesIds){
        try{
            roleService.updateRole(trole,nodesIds);
        }catch(Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }

    //删除
    @RequestMapping("deleteRole/{roleId}")
    @ResponseBody
    public boolean deleteRole(@PathVariable("roleId") Integer roleId){
        roleService.deleteRole(roleId);
        return true;
    }
}
